//
//  JDropDownListView.m
//  HeadIndustry
//
//  Created by jun on 2017/9/18.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JDropDownListView.h"

@interface JDropDownListView()<UITableViewDelegate , UITableViewDataSource,UIGestureRecognizerDelegate>


@end

@implementation JDropDownListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
//            NSLog(@"取消 View 的显示");
            if (_cancelBlock) {
                _cancelBlock();
            }
        }];
        tapGesture.delegate = self;
        [self addGestureRecognizer:tapGesture];
        
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    CGFloat width = self.width / 3.0;
    
    UITableView *tableViewObj = [[UITableView alloc] initWithFrame:CGRectMake(width , self.height, 0, 0) style:UITableViewStylePlain];
    tableViewObj.dataSource = self;
    tableViewObj.delegate = self;
    [self addSubview:tableViewObj];
    tableViewObj.bounces = NO;
    _tableView = tableViewObj;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (_dataArr.count > indexPath.row) {
        JVerdorProductTypeModel *typeModel = _dataArr[indexPath.row];
        cell.textLabel.text = typeModel.name;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_productTypeBlock) {
        JVerdorProductTypeModel *typeModel = _dataArr[indexPath.row];
        _productTypeBlock(typeModel);
    }
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
//    [self setConfigTableViewFrame];
    [self.tableView reloadData];
}

- (void)setConfigTableViewFrame
{
    if (_dataArr.count > 0) {
        CGFloat tableViewHeight = _dataArr.count * 44;
        if (tableViewHeight <= 0.6 * self.height) {
            self.tableView.frame = CGRectMake(self.width / 3.0, self.height - tableViewHeight, self.tableView.width, tableViewHeight);
        }
        else
        {
            self.tableView.frame = CGRectMake(self.width / 3.0, self.height - (0.6 * self.height), self.tableView.width, 0.6 * self.height);
        }
        
    }
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}

@end
